#!/bin/bash -e

# if input bam file has been sorted, output of samtools view -L should be sorted.

function info() {
echo Usage: `basename $0` 'in.bam in.bed'
exit 65
}

while getopts  ":s:p:t:" opts
do
        case  $opts  in
        s) sample_name=$OPTARG;;
		p) out_prefix=$OPTARG;;
		t) threads=$OPTARG;;
		\?) echo info;;
        esac
done
shift $(($OPTIND - 1))


if [ $# -lt 2 ]; then info; fi

if test -z "$threads"; then threads=3; fi

. $var


# bed=`basename $2`

if test `which samtools` = "/mnt/ilustre/app/medical/tools/samtools-1.2/samtools"; then echo `which samtools`; else echo bad samtools; exit 0; fi


samtools view -b -L$2 -@$threads $1 > $out_prefix.extract.bam && samtools index $out_prefix.extract.bam



. $cmd_done